package utils 
{
	
	// Keyboard/Mouse inputs
	import flash.utils.Dictionary;
	import net.flashpunk.Entity;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	
	/**
	 * ...
	 * @author Matt Hofmann
	 */
	public class KeyCombo extends Entity
	{
		// Timing variables - track which was the last key, and when it was pressed/released
		private var previouskeypressed:int = undefined;
		private	var ticksincerelease:Number = undefined;
		private var ticksincepress:Number = undefined;
		
		// constants, defining how 
		private const DOUBLE_TAP_PRESS_TIME:Number = 5;
		private const DOUBLE_TAP_RELEASE_TIME:Number = 8;
		
		// Mapping of key values to callback functions, for double-tap keying
		private var onDoubleKey:Dictionary = new Dictionary;
		
		public function KeyCombo() 
		{

		}
		
		public function addDoubleKeyListener(key:int, callback:Function):void 
		{
			onDoubleKey[key] = callback;
		}
		
		public function removeDoubleKeyListener(key:int):void 
		{
			onDoubleKey[key] = undefined;
		}
		
		override public function update():void
		{

			ticksincerelease++;
			if (Input.released(Key.ANY)) {
				ticksincerelease = 0;
			}
			
			ticksincepress++;
			if (Input.pressed(Key.ANY)) {			
				if (
					previouskeypressed == Input.lastKey
					&& ticksincerelease < DOUBLE_TAP_PRESS_TIME
					&& ticksincepress < DOUBLE_TAP_RELEASE_TIME
				) {
					if (onDoubleKey[Input.lastKey]) {
						onDoubleKey[Input.lastKey]();
					}
				}
				else {
					ticksincepress = 0;
				}
				
				previouskeypressed = Input.lastKey;
			}
		}
	}

}